//
//  TKIMConverseServiceView.m
//  test2
//
//  Created by TryHone on 2017/12/11.
//  Copyright © 2017年 TryHone. All rights reserved.
//

#import "TKIMConverseServiceView.h"
@interface TKIMConverseServiceView()
@property(nonatomic,strong) UIImageView* icon;
@property(nonatomic,strong) UILabel * title;
@property(nonatomic,strong) UILabel * line;
@property(nonatomic,strong) UILabel * redDot;
@end
@implementation TKIMConverseServiceView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self initUI];
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer * taps = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTap)];
        [self addGestureRecognizer:taps];
    }
    return self;
}
-(void)viewTap{
    if(_delegate && [_delegate respondsToSelector:@selector(toChat:converse:)]){
        TKIMUser *user = [[TKIMUser alloc] init];
        user.userID = _con.targetUser;
        user.nickName = _con.targetUserName;
        user.isToTop = _con.isToTop;
        user.userType = _con.chatUserType;
        [_delegate toChat:user converse:_con];
    }
}
-(void)initUI{
    CGRect rect = self.frame;
    [self addSubview:self.line];
    [self addSubview:self.icon];
    [self addSubview:self.title];
    [self addSubview:self.actionBtn];
    [self addSubview:self.redDot];
    self.line.frame = CGRectMake(0, 0, rect.size.width, 1);
    
    //初始化icon位置
    CGFloat iconMarginLeft = 14;
    CGFloat iconWidth = 60;
     CGFloat iconMarginTop = (rect.size.height - iconWidth)/2;
    self.icon.frame = CGRectMake(iconMarginLeft, iconMarginTop, iconWidth, iconWidth);

    
    //初始化actionBtn的位置
    CGFloat actionBntMarginRight = 10;
    CGFloat actionBtnWidth = 81;
    CGFloat actionBtnHeight = 28;
    CGFloat actionBtnMarginTop = (rect.size.height -actionBtnHeight)/2;
    CGFloat actionBtnX = rect.size.width - actionBntMarginRight - actionBtnWidth;
    self.actionBtn.frame = CGRectMake(actionBtnX, actionBtnMarginTop, actionBtnWidth, actionBtnHeight);
//    self.redDot.center = CGPointMake(actionBtnWidth-2, 2);
     self.redDot.center = CGPointMake(CGRectGetMaxX(self.actionBtn.frame)-1, CGRectGetMinY(self.actionBtn.frame)+2);
    [self bringSubviewToFront:self.redDot];
    
    //初始化title的位置
    CGFloat titleMarginTop = 5;
    CGFloat titleMarginLeft = 12;
    CGFloat titleMarginRight = 5;
    CGFloat titleWidth = rect.size.width - iconMarginLeft -iconWidth -titleMarginLeft -titleMarginRight -actionBtnWidth-actionBntMarginRight;
    CGFloat titleHeight = rect.size.height-titleMarginTop*2;
    self.title.frame = CGRectMake(CGRectGetMaxX(self.icon.frame)+titleMarginLeft, titleMarginTop, titleWidth, titleHeight);
}
#pragma mark--懒加载
-(UIImageView*)icon{
    if (!_icon) {
        _icon = [[UIImageView alloc] init];
    }
    return _icon;
}
-(UILabel*)title{
    if (!_title) {
        _title = [[UILabel alloc] init];
        _title.text = @"Hi,小智欢迎您的到来!";
        _title.textColor = [TKUIHelper colorWithHexString:@"#7b7b7b"];
        _title.numberOfLines = 0;
        _title.textAlignment = NSTextAlignmentLeft;
    }
    return _title;
}
-(UIButton*)actionBtn{
    if (!_actionBtn) {
        _actionBtn = [[UIButton alloc] init];
        [_actionBtn setTitle:@"点我咨询" forState:UIControlStateNormal];
        [_actionBtn setBackgroundColor:[UIColor clearColor]];
        [_actionBtn setTitleColor:[TKUIHelper colorWithHexString:@"#3e80c1"] forState:UIControlStateNormal];
        _actionBtn.layer.masksToBounds = YES;
        _actionBtn.layer.cornerRadius = 5 ;
        _actionBtn.layer.borderWidth = 1;
        _actionBtn.layer.borderColor =  [TKUIHelper colorWithHexString:@"#3e80c1"].CGColor;
        _actionBtn.clipsToBounds = NO;
    }
    return _actionBtn;
}
-(UILabel*)line{
    
    if (!_line) {
        _line = [[UILabel alloc] init];
        _line.backgroundColor = [TKUIHelper colorWithHexString:@"#e5e5e5"];
    }
    return _line;
}
-(UILabel*)redDot{
    if (!_redDot) {
        _redDot = [[UILabel alloc] init];
        _redDot.layer.backgroundColor = [UIColor redColor].CGColor;
        _redDot.size = CGSizeMake(8, 8);
        _redDot.layer.masksToBounds = YES;
        _redDot.layer.cornerRadius = 4;
        _redDot.hidden = YES;
    }
    return _redDot;
}
#pragma mark--属性设置
-(void)setCon:(TKIMConverseMessage *)con{
    _con = con;
    if (con.msgCount) {
        self.redDot.hidden = NO;
        [self bringSubviewToFront:self.redDot];
    }else{
        self.redDot.hidden = YES;
    }
    [_icon sd_setImageWithURL:[NSURL URLWithString:_con.photoUrl] placeholderImage:TKIMGetImage(@"kefu_head")];
}

@end
